//
//  TransitionCustom.cpp
//  Jump
//
//  Created by Zach Wang on 14-5-16.
//
//

#include "TransitionCustom.h"

//
// 扇形旋转向左转动
//

// The adjust factor is needed to prevent issue #442
// One solution is to use DONT_RENDER_IN_SUBPIXELS images, but NO
// The other issue is that in some transitions (and I don't know why)
// the order should be reversed (In in top of Out or vice-versa).
#define ADJUST_FACTOR 0.5f
TransitionSectorSlideInL::TransitionSectorSlideInL()
{
}

TransitionSectorSlideInL::~TransitionSectorSlideInL()
{
}

void TransitionSectorSlideInL::setSlideLeft(bool left){
    this->isLeft = left;
}

void TransitionSectorSlideInL::onEnter()
{
    TransitionScene::onEnter();
    
    _inScene->setAnchorPoint(Point(0.5f, -0.5f));
    _outScene->setAnchorPoint(Point(0.5f, -0.5f));
    
    //向左90度
    float rotate = -90;
    
    if(!this->isLeft) rotate = -rotate;
    
    // 让待进入的layer放在另一侧横躺
    _inScene->setRotation(-rotate);

    ActionInterval *rotation = (ActionInterval*)(Sequence::create
                                                 (
                                                  EaseElasticOut::create(RotateBy::create(_duration, rotate)),
                                                  nullptr
                                                  ));
    
    _outScene->runAction(rotation);
    _inScene->runAction
    (
     Sequence::create
     (
      rotation->clone(),
      CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
      nullptr
      )
     );
}

void TransitionSectorSlideInL::sceneOrder()
{
    _isInSceneOnTop = false;
}

void TransitionSectorSlideInL:: initScenes()
{
    Size s = Director::getInstance()->getWinSize();
    _inScene->setPosition( Point(-(s.width-ADJUST_FACTOR),0) );
}

TransitionSectorSlideInL* TransitionSectorSlideInL::create(float t, Scene* scene)
{
    TransitionSectorSlideInL* newScene = new TransitionSectorSlideInL();
    if(newScene && newScene->initWithDuration(t, scene))
    {
        newScene->autorelease();
        return newScene;
    }
    CC_SAFE_DELETE(newScene);
    return nullptr;
}

/**
 *  向右旋转
 */

TransitionSectorSlideInR::TransitionSectorSlideInR()
{
}

TransitionSectorSlideInR::~TransitionSectorSlideInR()
{
}

void TransitionSectorSlideInR::onEnter()
{
    this->TransitionSectorSlideInL::setSlideLeft(false);
    this->TransitionSectorSlideInL::onEnter();
}

void TransitionSectorSlideInR::sceneOrder()
{
    _isInSceneOnTop = false;
}

void TransitionSectorSlideInR:: initScenes()
{
    Size s = Director::getInstance()->getWinSize();
    _inScene->setPosition( Point(-(s.width-ADJUST_FACTOR),0) );
}

TransitionSectorSlideInR* TransitionSectorSlideInR::create(float t, Scene* scene)
{
    TransitionSectorSlideInR* newScene = new TransitionSectorSlideInR();
    if(newScene && newScene->initWithDuration(t, scene))
    {
        newScene->autorelease();
        return newScene;
    }
    CC_SAFE_DELETE(newScene);
    return nullptr;
}


TransitionCameraBezel::TransitionCameraBezel()
{
    bezelBackground = Sprite::create("appleBg.jpg");
}
TransitionCameraBezel::~TransitionCameraBezel()
{
    
}

TransitionCameraBezel* TransitionCameraBezel::create(float t, cocos2d::Scene *scene)
{
    TransitionCameraBezel* newScene = new TransitionCameraBezel();
    if (newScene&&newScene->initWithDuration(t, scene)) {
        newScene->autorelease();
        return newScene;
    }
    
    CC_SAFE_DELETE(newScene);
    return nullptr;
}
#define HEIGHT_ADJUST 10
void TransitionCameraBezel::onEnter()
{
    TransitionScene::onEnter();
    
    _inScene->setAnchorPoint(Point(0.5f, -0.5f));
    _outScene->setAnchorPoint(Point(0.5f, -0.5f));
    
    Size s = Director::getInstance()->getWinSize();
    
    float height = s.width/2 * tan(degree*3.1416/180);
    
    //上半部分
    ClippingNode* clipTop = ClippingNode::create();
    clipTop->setInverted(true);
    clipTop->setAlphaThreshold(1);
    bezelBackground->setAnchorPoint(Point(0.5f,0.5f));
    bezelBackground->setPosition(Point(s.width/2, s.height/2));
    DrawNode* topNode = DrawNode::create();
    Point rect[4] = {Point(0,0),Point(s.width,0),Point(s.width,s.height/2+height - HEIGHT_ADJUST),Point(0,s.height/2-height - HEIGHT_ADJUST)};
    topNode->drawPolygon(rect, 4, {0,0,0,0}, 0, {1,1,0,1});
    topNode->setPosition(Point(0,0));
    clipTop->setStencil(topNode);
    clipTop->addChild(bezelBackground);
    this->addChild(clipTop);
    
    //下半部分
    ClippingNode* clipBottom = ClippingNode::create();
    Sprite* another = Sprite::createWithTexture(bezelBackground->getTexture());
    another->setAnchorPoint(Point(0.5f,0.5f));
    another->setPosition(Point(s.width/2, s.height/2));
    DrawNode* bottomNode = DrawNode::create();
    Point rect2[4] = {Point(0,s.height/2-height),Point(s.width,s.height/2+height),Point(s.width,s.height),Point(0,s.height)};
    bottomNode->drawPolygon(rect2, 4, {0,0,0,0}, 0, {1,1,0,1});
    bottomNode->setPosition(Point(0,0));
    clipBottom->setStencil(bottomNode);
    clipBottom->addChild(another);
    clipBottom->setInverted(true);
    clipBottom->setAlphaThreshold(1);
    this->addChild(clipBottom);
    
    float topPosition = s.height/2 + abs(height);
    float bottomPosition = -topPosition;
    
    clipTop->setPositionY(topPosition);
    clipBottom->setPositionY(bottomPosition);
    
    ActionInterval* moveTop = Sequence::create(MoveTo::create(_duration/3, Point(0,0)), DelayTime::create(_duration), NULL);
    ActionInterval* moveBottom = Sequence::create(MoveTo::create(_duration/3, Point(0,0)),
                                                  DelayTime::create(_duration),
                                                  CallFunc::create(CC_CALLBACK_0(TransitionScene::finish,this)),
                                                  NULL);
    
    clipTop->runAction(moveTop);
    clipBottom->runAction(moveBottom);
    
}

void TransitionCameraBezel::setBezelBackground(cocos2d::Sprite *bezel)
{
    bezelBackground = bezel;
}

void TransitionCameraBezel::setBezelDegree(int degree)
{
    this->degree = degree % 360;
}

void TransitionCameraBezel::sceneOrder()
{
    _isInSceneOnTop = false;
}

void TransitionCameraBezel::initScenes()
{
    Size s = Director::getInstance()->getWinSize();
    _inScene->setPosition( Point(0+ADJUST_FACTOR,0) );
    
}
